//
//  ZCPublishTextViewController.m
//  百思不得姐
//
//  Created by Ning Xie on 16/9/4.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCPublishTextViewController.h"
#import "ZCAddTagViewController.h"
#import "ZCNavigationController.h"
@interface ZCPublishTextViewController ()

@property (weak, nonatomic) UIView *toolbarView;
@property (weak, nonatomic) UITextView *textView;

@end

@implementation ZCPublishTextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupNaviItem];
    [self setupUI];
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.textView becomeFirstResponder];

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    [self.textView resignFirstResponder];
}
-(void)setupNaviItem{
    
    self.navigationItem.title = @"发布帖子";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonAciton)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发表" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAciton)];
}
-(void)setupUI{
    
    //底部工具条
    UIView * toolbarView = [[UIView alloc]init];
    [self.view addSubview:toolbarView];
    self.toolbarView = toolbarView;
    
    [toolbarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.leading.trailing.equalTo(self.view);
    }];
    
    //工具条底部资源条
    UIView *resourceView = [[UIView alloc]init];
    [toolbarView addSubview:resourceView];
    resourceView.backgroundColor = ZCGlobalRBG;
    UIButton *oneBtn = [UIButton buttonWithBackgroundImage:@"post-@N" highlightedImage:@"post-@"];
    UIButton *twoBtn = [UIButton buttonWithBackgroundImage:@"post-#N" highlightedImage:@"post-#"];
    [resourceView addSubview:oneBtn];
    [resourceView addSubview:twoBtn];
    
    [resourceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.trailing.leading.equalTo(toolbarView);
        make.height.mas_equalTo(40);
    }];
    [oneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(resourceView);
        make.leading.equalTo(resourceView).offset(10);
    }];
    [twoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(oneBtn);
        make.leading.equalTo(oneBtn.mas_trailing).offset(20);
    }];
    
    //工具条顶部标签条
    UIView *tagView = [[UIView alloc]init];
    [toolbarView addSubview:tagView];
    UIButton *addBtn = [UIButton buttonWithBackgroundImage:@"tag_add_icon"];
    [tagView addSubview:addBtn];
    [addBtn addTarget:self action:@selector(addTagButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *addLab = [UILabel labelWithText:@"添加标签" textColor:[UIColor grayColor] fontSize:12 textAlignment:NSTextAlignmentLeft];
    [tagView addSubview:addLab];
    
    [tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(resourceView.mas_top);
        make.leading.trailing.equalTo(toolbarView);
    }];
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(tagView).offset(10);
        make.bottom.equalTo(tagView).offset(-10);
    }];
    [addLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(addBtn.mas_top).offset(-10);
        make.leading.equalTo(tagView).offset(10);
    }];
    [tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(addLab);
    }];
    [toolbarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tagView);
    }];
    
    //帖子内容部分
    UITextView *textView = [[UITextView alloc]init];
    textView.font = [UIFont systemFontOfSize:15];
    textView.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.view addSubview:textView];
    self.textView = textView;

    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuide);
        make.trailing.equalTo(self.view);
        make.leading.equalTo(self.view);
        make.bottom.equalTo(toolbarView.mas_top);
    }];
    UILabel *placeHolder = [UILabel labelWithText:@"这里添加文字,请勿发布色情、政治等违反国家法律的内容,违者封号处理." textColor:ZCGlobalRBG fontSize:15 textAlignment:NSTextAlignmentLeft];

    [textView addSubview:placeHolder];
    
    [placeHolder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textView).offset(8);
        make.leading.equalTo(self.view).offset(8);
        make.trailing.equalTo(self.view).offset(-8);
    }];
    
    
}

-(void)leftBarButtonAciton{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)rightBarButtonAciton{
    
    [self leftBarButtonAciton];
}
-(void)addTagButtonAction{
    
    ZCAddTagViewController *addTagVC = [[ZCAddTagViewController alloc]init];
    
    ZCNavigationController *nav = (ZCNavigationController*)[UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController;
    [nav pushViewController:addTagVC animated:YES];
    
}

/**
 *  监听键盘frame变化
 */
-(void)keyboardWillChangeFrame:(NSNotification*)noti{
    
    CGFloat duration = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    CGRect keyboardFrame = [noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    
    [self.toolbarView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(keyboardFrame.origin.y - self.view.height);
    }];
    
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];
    
}

@end
